<?php
class Topic
{
	public $_table_topic = 'topics';
	public $_table_topic_detail = 'topic_details';
	public $_table_category = 'categories_topic';
	private $_table_topic_fields = array('topic_id', 'category_id', 'topic_name', 'topic_title','topic_alias', 'topic_intro', 'topic_image', 'user_id', 'meta_keywords', 'meta_description','created', 'updated', 'ordering', 'active');
	private $_table_topic_details_fields = array('topic_id', 'topic_details', 'params');
	static protected $_instance = null;
	
	/**
	 * Get object instance
	 */
	
	public static function getInstance()
	{
		if( null === self::$_instance ) {
			self::$_instance = new self();	
		}
		return self::$_instance;
	}
	
	/**
	 * Get Topic
	 * @param int $id
	 * @param string $where
	 */
	
	public function getTopic($id, $where = null)
	{
		$storage = My_Globals::getStorage();
		$select = $storage->select()
					->from($this->_table_topic, '*')
					->join($this->_table_topic_detail, $this->_table_topic.'.topic_id = '.$this->_table_topic_detail.'.topic_id')
					->where($this->_table_topic.'.topic_id = ?', $id);
		if($where != null) {
			$select->where($where);
		}
		return $storage->fetchRow($select);
	}
	
	/**
	 * Get topic by alias
	 * @param string $alias
	 * @param string $where
	 */
	
	public function getTopicByAlias($alias, $where = null)
	{
		$storage = My_Globals::getStorage();
		$select = $storage->select()
					->from($this->_table_topic, '*')
					->join($this->_table_topic_detail, $this->_table_topic.'.topic_id = '.$this->_table_topic_detail.'.topic_id')
					->where($storage->quoteInto('topic_alias = ?', $alias));
		if($where != null) {
			$select->where($where);
		}
		return $storage->fetchRow($select);
	}
	
	/**
	 * Delete Topic
	 * @param int $id
	 */
	
	public function deleteTopic($id)
	{
		$storage = My_Globals::getStorage();
		if( $storage->delete($this->_table_topic, $storage->quoteInto('topic_id IN (?)', $id)) ) {
			return $storage->delete($this->_table_topic_detail, $storage->quoteInto('topic_id IN (?)', $id));
		}
		return false;
	}
	
	/**
	 * Save topic
	 * @param array $data
	 */
	
	public function saveTopic($data)
	{
		$storage = My_Globals::getStorage();
		$d1 = My_Globals::initData($this->_table_topic_fields, $data);
		$d2 = My_Globals::initData($this->_table_topic_details_fields, $data);
		if(isset($d1['topic_id']) && $d1['topic_id'] > 0) {
			// Update
			$rs = $storage->update($this->_table_topic, $d1, 'topic_id = '.$d1['topic_id']);
			if($rs) {
				$storage->update($this->_table_topic_detail, $d2, 'topic_id = '.$d1['topic_id']);
			}
		} else {
			// Insert
			$rs = $storage->insert($this->_table_topic, $d1);
			$lastId = $storage->lastInsertId();
			if($lastId) {
				$d2['topic_id'] = $lastId;
				$storage->insert($this->_table_topic_detail, $d2);
			} else {
				$rs = false;
			}
		}
		// Clean
		unset($d1);
		unset($d2);
		unset($data);
		// Return
		return $rs;
	}
	
	/**
	 * Get topic list
	 * @param string $where
	 * @param int $page
	 * @param int $limit
	 */
	
	public function getTopicList($page, $limit, $where = null, $order = null)
	{
		$storage = My_Globals::getStorage();
		// Set page = 1 
		if($page == 0) $page = 1;
		$select = $storage->select()
						->from($this->_table_topic, '*')
						//->join($this->_table_topic_detail, $this->_table_topic.'.topic_id = '.$this->_table_topic_detail.'.topic_id');
						->joinLeft($this->_table_category, $this->_table_topic.'.category_id = '.$this->_table_category.'.category_id');
		if($limit > 0) {
			$select->limitPage($page, $limit);
		}
		if($where != null) {
			$select->where($where);
		}
		if($order != null) {
			$select->order($order);
		} else {
			$select->order($this->_table_topic.'.topic_name ASC');
		}
		return $storage->fetchAll($select);
	}
	
	/**
	 * Get topic amount
	 * @param string $where
	 */
	
	public function getAmount($where = null)
	{
		$storage = My_Globals::getStorage();
		$select = $storage->select();
		$select->from($this->_table_topic, 'COUNT(*) AS total');
		if($where) $select->where($where);
		$row = $storage->fetchRow($select);
		return $row['total'];
	}
	
	public function aliasExist($str, $excludeId = 0)
	{
		$storage = My_Globals::getStorage();
		$select = $storage->select()
					->from($this->_table_topic, 'COUNT(*) AS total');
		$select->where($storage->quoteInto('topic_alias = ?', $str));
		if($excludeId > 0) {
			$select->where('topic_id <> ?', $excludeId);
		}
		$row = $storage->fetchRow($select);
		if($row['total'] > 0) {
			return true;
		} 
		return false;
	}
	
}